//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Validation Application Block
//===============================================================================

using System;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace EntLibContrib.Validation
{
	/// <summary>
	/// Externally configured Object Validator attribute
	/// </summary>
	[AttributeUsage(AttributeTargets.Property
	| AttributeTargets.Field
	| AttributeTargets.Method
	| AttributeTargets.Parameter,
	AllowMultiple = true,
	Inherited = false)]
	public sealed class ExternallyConfiguredObjectValidatorAttribute : ValidatorAttribute
	{
		#region Fields
		private readonly string targetRuleSet;
		private readonly string targetConfigFile;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the configuration file path.
		/// </summary>
		/// <value>The configuration file path.</value>
		public string ConfigurationFilePath
		{
			get { return targetConfigFile; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="ExternallyConfiguredObjectValidatorAttribute"/> class.
		/// </summary>
		/// <param name="configurationFilePath">The configuration file path.</param>
		public ExternallyConfiguredObjectValidatorAttribute(string configurationFilePath)
			: this(string.Empty, configurationFilePath)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ExternallyConfiguredObjectValidatorAttribute"/> class.
		/// </summary>
		/// <param name="ruleSet">The rule set.</param>
		/// <param name="configurationFilePath">The configuration file path.</param>
		public ExternallyConfiguredObjectValidatorAttribute(string ruleSet, string configurationFilePath)
		{
			targetConfigFile = configurationFilePath;
			targetRuleSet = ruleSet;
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Creates the <see cref="T:Microsoft.Practices.EnterpriseLibrary.Validation.Validator"/> described by the attribute object providing validator specific
		/// information.
		/// </summary>
		/// <param name="targetType">The type of object that will be validated by the validator.</param>
		/// <returns>
		/// The created <see cref="T:Microsoft.Practices.EnterpriseLibrary.Validation.Validator"/>.
		/// </returns>
		/// <remarks>This operation must be overriden by subclasses.</remarks>
		protected override Validator DoCreateValidator(Type targetType)
		{
			return new ExternallyConfiguredObjectValidator(targetRuleSet, targetConfigFile);
		}
		#endregion
	}
}
